import React from 'react'
import BaseComponent from 'basecomponent'
import './style.scss'
import LinkNavbar from 'link-navbar'
import AntTable from 'anttable'
import FuncButton from "func-button";
import apis from 'apis'
import {withRouter} from 'react-router'

class Company extends BaseComponent {
  constructor() {
    super(...arguments);
    this.state={
      universityData:[],
      recommendNum: 0,
      currentPage:1,
      pageSize:8,
      deleted: false,
      isLoadingUniversity:false
    }
  }

  componentDidMount() {
    this.getUniversityList();
  }

  shouldComponentUpdate(nextProps, nextState) {
    if(!nextProps.children && this.state.universityData.length !== this.state.recommendNum && !nextState.isLoadingUniversity){
      this.getUniversityList();
    }
    if(nextProps.children){
      nextProps.children.props.setNum = this.setNum.bind(this)
    }
    if(this.state.deleted){
      this.setState({deleted: false})
    }
    return true
  }

  setNum(num) {
    this.setState({recommendNum: num})
  }

  getUniversityList(){
    this.setState({isLoadingUniversity: true})
    this.universityRP && this.universityRP.request.abort()
    this.universityRP = apis.recommend.getRecommend(1); // channel keywords tag
    this.registerRequest(this.universityRP.request);
    this.universityRP.promise
      .then((res)=>{
        let data = res.Data || []
        data.map((item) => {
          let tags = item.Tags.split('|')
          item['levelFir'] = tags[0]
          item['batch'] = tags[1]
          item['addressFir'] = tags[2]
          item['category'] = tags[3]
        })
        res.Data&&this.setState({universityData: data, recommendNum: data.length, isLoadingUniversity: false});
      })
      .catch((err)=>{
        alert(err.msg)
        this.setState({isLoadingUniversity: false})
      })
  }

  topUniversity(text,record){
    let universityData = {}
    if(record.Order && record.Order === 1){
      universityData = Object.assign({}, record, {Order: 0})
    }else{
      universityData = Object.assign({}, record, {Order: 1})
    }
    this.reviseUniversityRP && this.reviseUniversityRP.request.abort()
    this.reviseUniversityRP = apis.university.reviseUniversity(universityData.Id, universityData)
    this.registerRequest(this.reviseUniversityRP.request)
    this.reviseUniversityRP.promise
      .then((res) => {
        alert('成功')
        this.getUniversityList()
      })
      .catch((err) => {
        alert(err.msg)
      })
  }

  deleteCompany(text,record){
    this.deleteRP && this.deleteRP.request.abort()
    this.deleteRP = apis.recommend.deleteRecommend(1, record.Id); // channel keywords tag
    this.registerRequest(this.deleteRP.request);
    this.deleteRP.promise
      .then(()=>{
          alert('删除成功')
          this.getUniversityList();
          this.setState({deleted: true})
      })
      .catch((err)=>{
        alert('删除出错:' + err.msg)
      })
  }


  render() {
    const {children} = this.props;
    const {universityData,currentPage,pageSize,} = this.state;
    const columns = [{
      title: '学校名称',
      dataIndex: 'Name',
      key: 'Name',
    }, {
      title: '学校级别',
      dataIndex: 'levelFir',
      colSpan: 2,
    },
      {
        title: '学校级别',
        dataIndex: 'levelSec',
        colSpan: 0,
      },
      {
        title: '学校批次',
        dataIndex: 'batch',
        key: 'batch',
      },
      {
        title:'学校区域',
        dataIndex:'addressFir',
        colSpan:2,
      },
      {
        title:'学校区域',
        dataIndex:'addressSec',
        colSpan:0,
      },

      {
        title:'学校类别',
        dataIndex:'category',
        key:'category'
      },
      {
        title: '操作',
        key: 'action',
        render: (text, record) => (
          <span>
          <a onClick={()=>{this.topUniversity(text,record)}}>
            {record.Order && record.Order === 1 ? '取消置顶' : '置顶'}
          </a>
            <span className="ant-divider"/>
            <a onClick={()=>{this.deleteCompany(text,record)}}>
            删除
          </a>
        </span>
        ),
      }];

    return (
      <div className={'sem-main-content'}>
        <div className={'recommend-company-page'}>
          <LinkNavbar props={this.props}/>
          {children ? children :
            <div className={'recommend-company-content'}>
              <FuncButton wrapperName={'recommend-company-FucBtn'}
                          leftText={'添加'}
                          leftTo={{pathname:'/home/recommend/company/add', state: {num: universityData.length}}}
                          showRight={false}
              />
              <AntTable columns={columns} data={universityData}
                        pagination={true} currentPage={currentPage} pageSize={pageSize}
                        onChange={(pagination)=>{
                          this.setState({
                            currentPage:pagination.current,
                            pageSize:pagination.pageSize,
                          })
                        }}/>
            </div>}
        </div>
      </div>
    );
  }
}

module.exports = withRouter(Company);
